<?

class General_View_Helper_FormDisplay extends Zend_View_Helper_Abstract {
	/**
	 * @var General_Form
	 */
	protected $form;

	static protected $instance = null;

	public function setView(Zend_View_Interface $view) {
		$this->view = $view;
		return $this;
	}
	public function setForm($form) {
		$this->form = $form;
	}

	public function FormDisplay(Zend_View_Interface $view =null, $form = null) {
		if (self::$instance == null) {
			self::$instance = new General_View_Helper_FormDisplay();
			self::$instance->setForm($form);
			self::$instance->setView($view);
		}
		return self::$instance;
	}

	public function start($action, $method = 'POST', $onsubmit = '', $append = '') {
		$params = array(
			'action' =>$action,
			'method' =>$method,
			'onsubmit' => $onsubmit,
			'append' => $append,
		);
		return $this->view->partial('form-display/start.phtml',$params);
	}

	public function close() {
		echo $this->view->partial('form-display/close.phtml');
	}
	public function text($name, $value, $label) {
		$params = array(
			'name'=>$name,
			'value'=>$value,
			'label'=>$label,
		);
		return $this->view->partial('form-display/text.phtml', $params);
	}
	public function submit($name, $label) {
		$params = array(
			'name'=>$name,
			'label'=>$label,
		);
		return $this->view->partial('form-display/submit.phtml', $params);
	}
	public function radio($name, $options, $checked, $defaultoption,  $label, $display_type = 'inline', $attribs = array()) {
		$params = array(
			'name'=>$name,
			'options'=>$options,
			'checked'=>$checked,
			'defaultoption'=>$defaultoption,
			'label'=>$label,
			'display_type'=>$display_type,
			'attribs'=>implode(' ',$attribs),
		);
		return $this->view->partial('form-display/radio.phtml', $params);
		
	}
	public function checkbox($name,$value,$checked,$label, $display_type = 'block',$attribs = array()) {
		$params = array(
			'name'=>$name,
			'value'=>$value,
			'checked'=>$checked,
			'label'=>$label,
			'display_type'=>$display_type,
			'attribs'=>implode(' ',$attribs),
		);
		return $this->view->partial('form-display/checkbox.phtml', $params);
	}
	public function multicheckbox($name,$options,$checked,$defaultoption,$label, $display_type = 'inline',$attribs = array()) {
		$params = array(
			'name'=>$name,
			'options'=>$options,
			'checked'=>$checked,
			'defaultoption'=>$defaultoption,
			'label'=>$label,
			'display_type'=>$display_type,
			'attribs'=>implode(' ',$attribs),
		);
		return $this->view->partial('form-display/multicheckbox.phtml', $params);
	}
	
	public function htmlareaOffice($name,$value,$label) {
		$params = array(
			'name'=>$name,
			'value'=>$value,
			'label'=>$label,
		);
		return $this->view->partial('form-display/htmlarea-office.phtml', $params);
	}
	
	public function textarea($name,$value, $label) {
		$params = array(
			'name'=>$name,
			'value'=>$value,
			'label'=>$label,
		);
		return $this->view->partial('form-display/textarea.phtml', $params);
	}
	public function image($name,$value, $label) {
		$params = array(
			'name'=>$name,
			'value'=>$value,
			'label'=>$label,
		);
		return $this->view->partial('form-display/image.phtml', $params);
	}
	
	public function render($data = null) {
		$str = '';
		foreach ($this->form->General_Form_Element as $element) {
			$valueName = $this->form->form_name . '_' . $element->formelement_name;
			if (isset($data->{$valueName})) {
				$value = $data->$valueName;
			} else {
				$value = '';
			}
			switch($element->formelement_view) {
				case 'htmlarea-office':
					$str .= $this->htmlareaOffice($element->formelement_name,$value,$element->formelement_label);
					break;
				case 'textarea':
					$str .= $this->textarea($element->formelement_name, $value, $element->formelement_label);
					break;
				case 'image':
					$str .= $this->image($element->formelement_name, $value, $element->formelement_label);
					break;
				default:
					$str .= $this->text($element->formelement_name,$value,$element->formelement_label);
					break;
			}
		}
		return $str;
	}

}